function dgrid ()
{
	this.uri='';
	this.showcolumns=true;
	this.showall=false;
	this.transform=null;
	this.mask=null;
	this.rows='';
	this.columns=[];
	this.title='';
	this.caption='';
	this.statline='';
	this.headertransform='';
	this.footertransform='';
	this.cellspacing=0;
	this.cellpadding=0;
	this.header='';
	this.bordercolor='#c9d7f1';
	this.headercolor='gainsboro';
	this.headerstyle='';
	this.footer='';
	this.selected='';
	this.page=1;
	this.editable=false;
	this.itemcount=0;
	this.pagesize=parseInt(window.config['pagesize']||25); 
	this.key='tbl';
	
	if (!window.datagrids) window.datagrids=[];
	this.id=window.datagrids.length;
	window.datagrids[this.id]=this;
	
	this.dgrid =function ()
	{
	}
	this.getRowByIDProperty = function (x)
	{
	    for (var row in this.rows) { if (this.rows[row].ID==x) return this.rows[row]; }
	}
	this.getRowByIDValue = function (x)
	{
	    for (var row in this.rows) { if (this.rows[row]['ID']==x) return this.rows[row]; }
	}
	this.draw =function ()
	{  
		this.itemcount = this.rows.length;
		
	    var htk = '';
	    var k   = '';
		var htm = '';
		
		htm+= '<form method=post action="' + location.href + '">'; 
		htm+= '<table id="'+this.key+'" cellspacing=0 bgcolor="'+this.bordercolor+'"'; 
		htm+= ' width="100%" style="border-bottom:solid 1px #999"><tr><td valign=top style="padding:10px 0px 10px 10px">';
		
		if (this.headertransform) htm+= '' + this.headertransform();
		else htm+= '' + this.header;
		
		htm+= '<div id="dc'+this.key+'" style="overflow:auto;background-color:#f0f0f0;border:solid 1px;;border-color:#999 #fff #fff #999">';
		htm+= '<table cellspacing='+this.cellspacing+' cellpadding='+this.cellpadding+' bgcolor=white width="100%">';
 
		
		if (this.showcolumns && this.columns.length > 0)
		{
			htm+= '<tr>';  
			for (var column in this.columns)
				htm+= '<td style="'+this.headerstyle+'" nowrap bgcolor="'+this.headercolor+'">&nbsp;' + this.columns[column] + '&nbsp;</td>';   
			htm+= '<td style="'+this.headerstyle+'" bgcolor="'+this.headercolor+'" width=100%>&nbsp;</td>';
			htm+= '</tr>';
		}
		
		if (this.firstrowtransform) htm+= this.firstrowtransform();
		
		if (this.rows.length==0)
		{    
			if (window.http.readyState==ReadyState.DONE)
			{
				htm += 'Search is complete. No records were found.'; 
			} 
			else
			{
				htm += 'Processing your request. Please stand by...';
			}
		}
		else
		{
			for (var row=0;row<this.rows.length;row++) 
			{ 
				var p=Math.ceil((row+1)/this.pagesize);
				if (p==this.page || this.showall)
				{
					if (this.transform)
					{ 
						htm+= this.transform(this.rows[row]);
					}
					else if (this.mask)
					{
						htk = this.mask;
						for (var o=0;o<this.rows[row].length;o++)
						{
							k='{' + this.columns[o] + '}';
							while (htk.indexOf (k)>=0)
								htk=htk.replace (k, this.rows[row][this.columns[o]].substr(0,50));
						}
						htm+= htk;
					}
					else
					{
						htm+= '<tr>';
						for (var col in this.rows[row])    
						{
						    var value = this.rows[row][col];
							htm += '<td nowrap class=datarow '+(this.editable?'onclick="window.datagrids['+this.id+'].select('+row+')"':'')+'>';
							if (this.editable&&row==this.selected) htm += '<input value="' + value + '">';
							else htm += '' + (value.length>0?value:'&nbsp;') + '';
							htm += '</td>';
						}
						htm+= '<td width=100% class=datarow>&nbsp;</td>';
						htm+= '</tr>';
					}
				} 
			}
			if (this.lastrowtransform) htm+= this.lastrowtransform();
		}

		htm+= '</table>';
		htm+= '</div>';
		if (this.footertransform) htm+= '' + this.footertransform();
		else htm+= '' + this.footer; 
		htm+= '</td></tr></table>';
		htm+= '</form>';
		return htm;
	}
	
	this.select = function (i)
	{ 
	    this.selected=i;
	    Controller.Reload ();
	}
}
dgrid.EXPANDED='expanded';
dgrid.LIST='list';
dgrid.INVOICE='invoice';
